<?php
require_once(SITE_ROOT . 'class/db/DB.class.php');
require_once(SITE_ROOT . 'class/bean/Training.class.php');

class DBTraining {
	
	public function getTraining($trainingtype = null, $country = null, $date = null, $orderby = null) {
		$params = array();
		
		$db = new DB();
		$connection = $db->connect();
		
		$trainings = new ArrayObject();
				
		$select = "SELECT t.`trainingtype#`, t.`country#`, t.`date#`, t.`number`, t.`fraction`, tt.`abbreviation`, tt.`description_en` FROM `ttraining` t, `ttrainingtype` tt ";
		$where = "WHERE t.`trainingtype#` = tt.`trainingtype#` ";
		if (isset($trainingtype) || isset($country) || isset($date)) {
			if (isset($trainingtype)) {
				$where .= "AND `trainingtype#` = ? ";
				$params[] = $trainingtype->getTrainingTypeID();
			}
			if (isset($country)) {
				$where .= "AND `country#` = ? ";
				$params[] = $country->getCountryID();
			}
			if (isset($date)) {
				$where .= "AND `date#` = ? ";
				$params[] = date("Y-m-d", $date);
			}
		}		
		
		if ($stmt = $connection->prepare($select . $where)) {
			$stmt->execute($params);
			
			$stmt->bindColumn(1, $trainingtypeID);
			$stmt->bindColumn(2, $countryID);
			$stmt->bindColumn(3, $date);
			$stmt->bindColumn(4, $number);
			$stmt->bindColumn(5, $fraction);
			$stmt->bindColumn(6, $abbreviation);
			$stmt->bindColumn(7, $description);
			
			while ($stmt->fetch()) {
				$trainingtype = new TrainingType($trainingtypeID, $abbreviation, $description);
				if (!isset($country)) $country = new Country($countryID);
				$training = new Training($trainingtype, $country, $date, $number, $fraction);
				$trainings->append($training);
			}
		}		
		return $trainings;
	}
	
	public function saveTraining($training) {
		$db = new DB();
		$connection = $db->connect();
		
		if ($stmt = $connection->prepare('INSERT INTO `ttraining` (`trainingtype#`, `country#`, `date#`, `number`, `fraction`) VALUES (?, ?, ?, ?, ?) ON DUPLICATE KEY UPDATE `number`=VALUES(`number`), `fraction`=VALUES(`fraction`)')) {
			$trainingtypeID = $training->getTrainingType()->getTrainingTypeID();
			$countryID = $training->getCountry()->getCountryID();
			$date = date('Y-m-d', $training->getDate());
			$number = $training->getNumber();
			$fraction = $training->getFraction();
			
			$stmt->execute(array($trainingtypeID, $countryID, $date, $number, $fraction));
		}
	}
	
	public function saveTrainings($trainings) {
		$db = new DB();
		$connection = $db->connect();
		
		if ($stmt = $connection->prepare('INSERT INTO `ttraining` (`trainingtype#`, `country#`, `date#`, `number`, `fraction`) VALUES (?, ?, ?, ?, ?) ON DUPLICATE KEY UPDATE `number`=VALUES(`number`), `fraction`=VALUES(`fraction`)')) {
			foreach ($trainings as $training) {
				$trainingtypeID = $training->getTrainingType()->getTrainingTypeID();
				$countryID = $training->getCountry()->getCountryID();
				$date = date('Y-m-d', $training->getDate());
				$number = $training->getNumber();
				$fraction = $training->getFraction();
				
				$stmt->execute(array($trainingtypeID, $countryID, $date, $number, $fraction));
			}
		}
	}
	
	public function getLatestDate() {
		$db = new DB();
		$connection = $db->connect();
		
		$select = "SELECT MAX(`date#`) FROM `ttraining`";
		
		if ($stmt = $connection->prepare($select)) {
			$stmt->execute();
			
			$stmt->bindColumn(1, $date);
			
			while ($stmt->fetch()) {
				$maxdate = strtotime($date);
			}
		}		
		return $maxdate;
	}
		
}

?>